// svgrect.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#import <MSXML6.dll>



//#include <msxml6.h>
using namespace std;
using namespace MSXML2;

IXMLDOMDocument2Ptr pXMLDoc;
IXMLDOMElementPtr pNewNode1;
IXMLDOMElementPtr pNewNode2;
IXMLDOMElementPtr pRootNode;
IXMLDOMElementPtr pDocType;
IXMLDOMAttributePtr pSchemaLocationAttribute;

void createRect(_bstr_t x, _bstr_t y, _bstr_t fill, _bstr_t stroke, _bstr_t stroke_width, _bstr_t width, _bstr_t height)
{
	pNewNode1 = pXMLDoc->createElement(_T("rect"));
	pNewNode1->setAttribute(_T("x"), x);
	pNewNode1->setAttribute(_T("y"), y);
	pNewNode1->setAttribute(_T("fill"), fill);
	pNewNode1->setAttribute(_T("stroke"), stroke);
	pNewNode1->setAttribute(_T("stroke-width"), stroke_width);
	pNewNode1->setAttribute(_T("width"), width);
	pNewNode1->setAttribute(_T("height"), height);
	pRootNode->appendChild(pNewNode1);

}

//createLine("#010101", "#108140", "5", "round", "31", "181", "81", "41");
//setting->c++->language->No (/Zc:wchar_t-)
void createLine(_bstr_t fill, _bstr_t stroke, _bstr_t stroke_width, _bstr_t stroke_linecap, _bstr_t x1, _bstr_t y1, _bstr_t x2, _bstr_t y2)
{
	pNewNode2 = pXMLDoc->createElement(_T("line"));
	pNewNode2->setAttribute(_T("fill"), fill);
	pNewNode2->setAttribute(_T("stroke"), stroke);
	pNewNode2->setAttribute(_T("stroke-width"), stroke_width);
	pNewNode2->setAttribute(_T("stroke-linecap"), stroke_linecap);
	pNewNode2->setAttribute(_T("x1"), x1);
	pNewNode2->setAttribute(_T("y1"), y1);
	pNewNode2->setAttribute(_T("x2"), x2);
	pNewNode2->setAttribute(_T("y2"), y2);
	pNewNode1->appendChild(pNewNode2);
}

int main(int argc, char* argv[])
{	
	if (argc != 3)
	{
		cerr << "Fehler: 3 Argument";
		return 1;
	}

	HRESULT hResult = S_OK;	
	hResult = CoInitialize(NULL);
	if (FAILED(hResult))
	{
		cerr << "Failed to initialize COM environment" << endl;
		return 0;
	}
	try
	{
		//struct IDispatch * pDispatch;
		//hResult = pXMLDoc.CreateInstance(_T("Msxml2.DOMDocument.3.0"));


		hResult = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));
		if FAILED(hResult)
		{
			cerr << "Failed to create XML document instance" << endl;
			return 1;
		}
		pXMLDoc->setProperty(_T("ProhibitDTD"), VARIANT_FALSE);
		pXMLDoc->validateOnParse = VARIANT_FALSE;
		
		IXMLDOMProcessingInstructionPtr pXMLProcessingNode = 
			pXMLDoc->createProcessingInstruction(_T("xml"), _T(" version='1.0' encoding='UTF-8'"));
		pXMLDoc->appendChild(pXMLProcessingNode);
		
		//pXMLDoc->loadXML("<?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
		//<!ENTITY ns_svg "http://www.w3.org/2000/svg">
		//<!ENTITY ns_xlink "http://www.w3.org/1999/xlink">
		//]><svg></svg>");

	/*		
		if(pXMLDoc->loadXML(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
							   <svg version=\"1.1\" id=\"Ebene_1\" xmlns=\"&ns_svg;\" width=\"302\" height=\"252\" viewBox=\"0 0 302 252\"\n\
							   overflow=\"visible\" enable-background=\"new 0 0 302 252\" xml:space=\"preserve\"></svg>")) == VARIANT_FALSE)
		{
			cerr << "Failed to load XML document" << endl;
			return 1;
		}
	*/		

		
		/*
		if(pXMLDoc->loadXML(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\
							   <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" [\n\
							   <!ENTITY ns_svg \"http://www.w3.org/2000/svg\">\n\
							   <!ENTITY ns_xlink \"http://www.w3.org/1999/xlink\">\n\
							   ]>\n\
							   <svg  version=\"1.1\" id=\"Ebene_1\" xmlns=\"&ns_svg;\" xmlns:xlink=\"&ns_xlink;\" width=\"302\" height=\"252\" viewBox=\"0 0 302 252\"\n\
							   overflow=\"visible\" enable-background=\"new 0 0 302 252\" xml:space=\"preserve\"></svg>")) == VARIANT_FALSE)
		{
			cerr << "Failed to load XML document" << endl;
			return 1;
		}
		*/
		/*
		
		if(pXMLDoc->load(_T("c:\\test.xml")) == VARIANT_FALSE)
		{
			cerr << "Failed to load XML document" << endl;
			return 1;
		}
		*/


		//pDocType = pXMLDoc->createElement(_T("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" [ <!ENTITY ns_svg \"http://www.w3.org/2000/svg\"><!ENTITY ns_xlink \"http://www.w3.org/1999/xlink\">]>"));
		//pXMLDoc->appendChild(pDocType);
		//bool testBool = pXMLDoc->loadXML(_T("<?xml version=\"1.0\" encoding=\"utf-8\"?><svg></svg>"));
		
		pRootNode = pXMLDoc->createElement(_T("svg"));
		pXMLDoc->appendChild(pRootNode);
 
		// Add the schema attributes to the Root Element
		
		//pRootNode = pXMLDoc->selectSingleNode(_T("/svg"));
		pRootNode->setAttribute(_T("version"), _T("1.0"));
		pRootNode->setAttribute(_T("id"), _T("Ebene_1"));
		//pRootNode->setAttribute(_T("xmlns"), _T("http://www.w3.org/2000/svg"));
		//pRootNode->setAttribute(_T("xmlns:xlink"), _T("http://www.w3.org/1999/xlink"));
		pRootNode->setAttribute(_T("width"), _T("302"));
		pRootNode->setAttribute(_T("height"), _T("252"));
		pRootNode->setAttribute(_T("viewBox"), _T("0 0 302 252"));
		
		//<rect x="1" y="1" fill="none" stroke="#3A53A4" stroke-width="2" width="300" height="250"/>
		createRect("1", "1", "none", "#3A53A4", "2", argv[1], argv[2]);
		/*
		pNewNode1 = pXMLDoc->createElement(_T("rect"));
		pNewNode1->setAttribute(_T("x"), _T("1"));
		pNewNode1->setAttribute(_T("y"),_T("1"));
		pNewNode1->setAttribute(_T("fill"), _T("none"));
		pNewNode1->setAttribute(_T("stroke"),_T("#3A53A4"));
		pNewNode1->setAttribute(_T("stroke-width"),_T("2"));
		pNewNode1->setAttribute(_T("width"),_T("300"));
		pNewNode1->setAttribute(_T("height"),_T("250"));
		pRootNode->appendChild(pNewNode1);
		*/
		pNewNode1 = pXMLDoc->createElement(_T("g"));
		pRootNode->appendChild(pNewNode1);

		//createLine("line");
		createLine("#010101", "#108140", "5", "round", "31", "181", "81", "41");

		//<line fill="#010101" stroke="#108140" stroke-width="5" stroke-linecap="round" x1="31" y1="181" x2="81" y2="41"/>
		/*
		_bstr_t x = "line";
		pNewNode2 = pXMLDoc->createElement(x);
		pNewNode2->setAttribute(_T("fill"), _T("#010101"));
		pNewNode2->setAttribute(_T("stroke"),_T("#108140"));
		pNewNode2->setAttribute(_T("stroke-width"),_T("5"));
		pNewNode2->setAttribute(_T("stroke-linecap"),_T("round"));
		pNewNode2->setAttribute(_T("x1"),_T("31"));
		pNewNode2->setAttribute(_T("y1"),_T("181"));
		pNewNode2->setAttribute(_T("x2"),_T("81"));
		pNewNode2->setAttribute(_T("y2"),_T("41"));
		pNewNode1->appendChild(pNewNode2);
		*/
		// be created as Nodes, then set
		//_variant_t varType((short)MSXML2::tagDOMNodeType::NODE_ATTRIBUTE);
		//pSchemaLocationAttribute = pXMLDoc->createNode(
		//	varType,_T("xsi:noNamespaceSchemaLocation"),
		//	_T("http://www.w3.org/2001/XMLSchema-instance"));
		//pRootNode->setAttributeNode(pSchemaLocationAttribute);
		
		//pRootNode->setAttribute(_T("xsi:noNamespaceSchemaLocation"), _T("SampleSchema.xsd"));

		
		//if(pXMLDoc->loadXML(_T("<Parent></Parent>")) == VARIANT_FALSE)
		//{
		//	cerr << "Failed to load XML document" << endl;
		//	return 1;
		//}

		//hResult = pXMLDoc->
		
		hResult = pXMLDoc->save(_T("c:\\test.svg"));
		if FAILED(hResult)
		{
			cerr << "Failed to save test document" << endl;
			return 1;
		}

	}
	catch (_com_error &e)
	{
		cerr << "COM Error" << endl;
		cerr << "Message = " << e.ErrorMessage() << endl;
		return 1;

	}
	

	/*
	if (argc != 2)
	{
		cout << "Fehler: Es gibt 2 Argument(Hoehe und Breite)";
	}
	createXML();
	putRectangleIntoXML();
	XML->saveToFile("C:\hallo.svg");
	cout << argc << " Argumente festgestellt" << endl;
    for ( int i=0; i < argc; i++ ) {
        cout << argv[i] << endl;
    }
	ofstream myfile;
	myfile.open ("example.svg");


	myfile << "<?xml version=\"1.0\" encoding=\"utf-8\"?>"<<endl
		<< "<!-- Generator: Adobe Illustrator 12.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 51448)  -->" << endl
		<< "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\" [" << endl
		<< "	<!ENTITY ns_svg \"http://www.w3.org/2000/svg\">" <<endl
		<< "	<!ENTITY ns_xlink \"http://www.w3.org/1999/xlin8k\">" <<endl
		<< "]>" << endl
		<< "<svg  version=\"1.1\" id=\"Ebene_1\" xmlns=\"&ns_svg;\" xmlns:xlink=\"&ns_xlink;\" width=\"302\" height=\"252\" viewBox=\"0 0 302 252\"" << endl
		<< "	 overflow=\"visible\" enable-background=\"new 0 0 302 252\" xml:space=\"preserve\">" <<endl
		<< "<rect x=\"1\" y=\"1\" fill=\"none\" stroke=\"#3A53A4\" stroke-width=\"2\" width=\""<<argv[1]<<"\" height=\""<<argv[2]<<"\"/>" <<endl
		<< "<g>" <<endl
		<< "	<line fill=\"#010101\" stroke=\"#108140\" stroke-width=\"5\" stroke-linecap=\"round\" x1=\"31\" y1=\"181\" x2=\"81\" y2=\"41\"/>" << endl
		<< "</g>" <<endl
		<< "</svg>" << endl;
	myfile.close();
	int c;
	cin>>c;
	return 0;
	*/
	/*
	IXMLDOMDocument2Ptr pXMLDoc;
	HRESULT hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument60));      
	if(FAILED(hr))
	{
		cout<<"Could not create instance"<<endl;
		return 1;
	}
	*/
	int c;
	cin >> c;
	return 0;
}


